﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
using YXQ.Common.Helper;
using System.Data;
namespace YXQ.Dao
{

    public class ItemDao
    {


        public ItemDao()
        {

            Mapper.CreateMap<Y_Item, ItemModel>();
        }
        public List<Y_Item> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_Item.Count();

                return edm.Y_Item.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ItemModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Item.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_Item, ItemModel>(query);
            }

        }


        public int Save(ItemModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Item.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_Item();

                    edm.AddToY_Item(query);

                }


                query.ProjectTitle = obj.ProjectTitle;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Item.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }

        public List<ItemModel> GetList(int proid)
        {
            var sql = "select a.id,b.projectid,b.itemid,b.status,b.remark,a.projecttitle from Y_Item a left join y_daitem b on a.Id=b.itemid and b.projectid=" + proid;
           DataTable dt=DBHelper.ExecuteDataSet(sql);
           if (dt.Rows.Count > 0) {
               var query = from s in dt.AsEnumerable()
                           select new ItemModel() { 
                                Id=s.Field<int>("id"),
                                ItemId=s.Field<int?>("ItemId"),
                                Status = s.Field<int?>("Status"),
                                Remark = s.Field<string>("Remark"),
                                ProjectId = s.Field<int?>("projectid"),
                                ProjectTitle = s.Field<string>("projecttitle")
                           };
               return query.ToList();
           }
           return null;
            //using (YXQManageEntities edm = new YXQManageEntities())
            //{
            //    var query = from s in edm.Y_Item
            //                from d in edm.Y_DaItem
            //                where s.Id == d.ItemId && d.ProjectId == proid
            //                select new ItemModel() { 
            //                   Id=s.Id,
            //                   ItemId=d.ItemId,
            //                   ProjectTitle=s.ProjectTitle,
            //                   ProjectId=d.ProjectId,
            //                   Remark=d.Remark,
            //                   Status=d.Status,
            //                };
            //    return query.ToList();

            //}
        }



    }

}

